package com.minutemouse.generator;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

public class GeneratorActivty extends Activity{

	private Generator gen;
	private TabHost tabHost;
	private TabSpec spec3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
          
        setContentView(R.layout.activity_power_gen);
        
        Intent intent = getIntent();

        tabHost = (TabHost)findViewById(R.id.tabhost);
        tabHost.setup();
        
        TabSpec spec1 = tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.id.tab1);
        spec1.setIndicator("Tab 1", getResources().getDrawable(R.drawable.btn_add));
        
        TabSpec spec2 = tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Tab 2");
        spec2.setContent(R.id.tab2);
        
        spec3 = tabHost.newTabSpec("Tab 3");
        spec3.setIndicator("Tab 3");
        spec3.setContent(R.id.tab3);
        
        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);
        
        Spinner powerSpinner = (Spinner) findViewById(R.id.powerType);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.power_types, 
        		android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        powerSpinner.setAdapter(adapter);
        
        Spinner actionSpinner = (Spinner) findViewById(R.id.actionType);
        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.action_types, 
        		android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        actionSpinner.setAdapter(adapter2);
        
        
        gen = new Generator(getBaseContext());
        
    }
    
    
    public void generateImage(View v) {
    	
    	
    	ArrayList<EditText> EtextArray = new ArrayList();
    	
    	EtextArray.add((EditText)findViewById(R.id.editName));
    	EtextArray.add((EditText)findViewById(R.id.editAttributes));
    	EtextArray.add((EditText)findViewById(R.id.editRange));
    	EtextArray.add((EditText)findViewById(R.id.editTarget));
    	EtextArray.add((EditText)findViewById(R.id.editAttack));
    	EtextArray.add((EditText)findViewById(R.id.editHit));
    	EtextArray.add((EditText)findViewById(R.id.editEffect));
    	    	
    	Spinner powerSpinner = (Spinner)findViewById(R.id.powerType);
    	Spinner actionSpinner = (Spinner)findViewById(R.id.actionType);
    	String powerTp = powerSpinner.getSelectedItem().toString();
    	String actionTp = actionSpinner.getSelectedItem().toString();
    	
    	ArrayList<String> textArray = new ArrayList<String>();
    	
    	for (int i = 0; i < EtextArray.size(); i ++) {
    		textArray.add(EtextArray.get(i).getText().toString());
    	}
    	
    	textArray.add(powerTp);
    	textArray.add(actionTp);
    	
    	gen.setValues(textArray);
    	
    	
    	
    	gen.generateImg();
    	
    	ImageView imgview = (ImageView)findViewById(R.id.imageView1);
    	imgview.setImageBitmap(gen.getImage());
    	//tabHost.setCurrentTab(2);
    	//tabHost.getCurrentView().draw(new Canvas(gen.getImage()));
    	
    	
    }
    
    	
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_power_gen, menu);
        return true;
    }
    
    // Here we save things that like to get lost in state changes... such as rotation.
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    	super.onSaveInstanceState(savedInstanceState);
    	
    	savedInstanceState.putParcelable("genImage", gen.getImage());
    	savedInstanceState.putInt("currentTab", tabHost.getCurrentTab());
    }
    
    
    // And here we override the method to allow us to resume in our previous state!
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	
    	ImageView imgview = (ImageView)findViewById(R.id.imageView1);
    	imgview.setImageBitmap((Bitmap) savedInstanceState.getParcelable("genImage"));
    	tabHost = (TabHost)findViewById(R.id.tabhost);
    	tabHost.setCurrentTab(savedInstanceState.getInt("currentTab"));
    }
    
    
}
